﻿using BUSINESS;
using DataAccessObject;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace GUI
{
    /// <summary>
    /// Interaction logic for GUIChonGiaoVien.xaml
    /// </summary>
    public partial class GUIChonGiaoVien : Window
    {
        public GIAOVIEN GiaoVien;
        private List<GIAOVIEN> listGiaoVien;
        private List<GIAOVIEN> listNoneChuNhiem;
        private List<GIAOVIEN> listCurrentGiaoVien = new List<GIAOVIEN>();
        private NAMHOC namhoc = new NAMHOC();
        private string filter = string.Empty;

        public GUIChonGiaoVien()
        {
            Initialize();
        }

        private void Initialize()
        {
            InitializeComponent();
            this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            InitListGiaoVien();
            this.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;
        }

        public GUIChonGiaoVien(NAMHOC namhoc)
        {
            this.namhoc = namhoc;
            Initialize();
        }

        public string Filter
        {
            get
            {
                return this.filter.ToUpperInvariant();
            }
            set
            {
                filter = value;
                List<GIAOVIEN> list;
                if (txtBoChonChuNhiem.IsChecked == false)
                    list = listGiaoVien;
                else
                    list = listNoneChuNhiem;
                var query = from data in list
                            where data.MaGiaoVien.ToUpperInvariant().Contains(Filter) ||
                            data.HoTen.ToUpperInvariant().Contains(Filter)
                            select data;
                listCurrentGiaoVien.Clear();
                listCurrentGiaoVien.AddRange(query.ToList());
                txtListGiaoVien.Items.Refresh();
            }
        }

        private void InitListGiaoVien()
        {
            ///////////////////////////////////////////
            listGiaoVien = GiaoVienBUS.getAllGiaoVien();    
            ///////////////////////////////////////////
            listNoneChuNhiem = GiaoVienBUS.getAllNoneChuNhiem(namhoc);
            listCurrentGiaoVien.AddRange(listNoneChuNhiem);
            //////////////////////////////////////////
            txtListGiaoVien.ItemsSource = listCurrentGiaoVien;
            if (txtListGiaoVien.Items.Count > 0) txtListGiaoVien.SelectedIndex = 0;
        }

        private void txtListGiaoVien_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            GiaoVien = txtListGiaoVien.SelectedItem as GIAOVIEN;
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void txtBoChonChuNhiem_Click(object sender, RoutedEventArgs e)
        {
            if (txtBoChonChuNhiem.IsChecked == true)
                txtBoChonChuNhiem_checked();
            else
                txtBoChonChuNhiem_unchecked();
        }

        private void txtBoChonChuNhiem_checked()
        {
            listCurrentGiaoVien.Clear();
            listCurrentGiaoVien.AddRange(listNoneChuNhiem);
            txtListGiaoVien.Items.Refresh();
        }

        private void txtBoChonChuNhiem_unchecked()
        {
            listCurrentGiaoVien.Clear();
            listCurrentGiaoVien.AddRange(listGiaoVien);
            txtListGiaoVien.Items.Refresh();
        }
    }
}
